#include "kugouapp.h"
#include "./ui_kugouapp.h"
#include"columnwidget.h"
#include"musicitemwidget.h"
#include<QMediaPlayer>
#include<QAudioOutput>
#include<QFileDialog>
#include<QStandardPaths>
#include<QMediaMetaData>
#include<QPixmap>
#include<QImage>

KuGouApp::KuGouApp(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::KuGouApp),
    player(std::make_unique<QMediaPlayer>()),
    audio_output(std::make_unique<QAudioOutput>()),
    is_play(false)
{
    ui->setupUi(this);

    this->setWindowFlag(Qt::FramelessWindowHint);

    ui->search_lineEdit->addAction(QIcon("://image/titlebar/search-black.svg"),QLineEdit::LeadingPosition);

    InitPosters();
    InitColumnList();

    this->player->setAudioOutput(this->audio_output.get());

    connect(player.get(),&QMediaPlayer::positionChanged,this,&KuGouApp::updateSliderPosition);
    connect(player.get(),&QMediaPlayer::durationChanged,this,&KuGouApp::updateSliderRange);
    connect(player.get(),&QMediaPlayer::metaDataChanged,[this](){
        QMediaMetaData data=this->player->metaData();

        QString title=data.value(QMediaMetaData::Title).toString();
        auto cover=data.value(QMediaMetaData::CoverArtImage).value<QImage>();

        ui->cover_label->setPixmap(QPixmap::fromImage(cover));
        ui->song_name_label->setText(title);
    });
}

KuGouApp::~KuGouApp()
{
    delete ui;
}

void KuGouApp::setPlayMusic(QUrl url)
{
    this->player->setSource(url);
    ui->horizontalSlider->setValue(0);
}

void KuGouApp::updateSliderPosition(qint64 position)
{
    ui->horizontalSlider->setValue(position);
}

void KuGouApp::updateSliderRange(qint64 duration)
{
    ui->horizontalSlider->setRange(0,duration);
}

void KuGouApp::InitPosters()
{
    ui->advertising_board_widget->addPoster(QPixmap("://image/poster/1.jpg"));
    ui->advertising_board_widget->addPoster(QPixmap("://image/poster/2.jpg"));
}

void KuGouApp::InitColumnList()
{
    QVBoxLayout* layout=dynamic_cast<QVBoxLayout*>( ui->tab_widget->layout());
    if(!layout)
        return;

    layout->insertWidget(layout->count()-1, new ColumnWidget("潮流音乐站",this));
    layout->insertWidget(layout->count()-1, new ColumnWidget("热门好歌精选",this));
    layout->insertWidget(layout->count()-1, new ColumnWidget("私人专属好歌",this));
}

void KuGouApp::on_basic_toolButton_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->main_page);
}


void KuGouApp::on_local_download_toolButton_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->local_download_page);
}


void KuGouApp::on_local_add_toolButton_clicked()
{
    QString musicPath=QStandardPaths::standardLocations(QStandardPaths::MusicLocation).first();
    QString path=QFileDialog::getOpenFileName(this,"添加音乐",musicPath,"Music (*.mp3 *.aac *.wav)");
    if(path.isEmpty())
        return;

    this->local_music_map[this->local_music_map.size()+1]=QUrl::fromLocalFile(path);

    QVBoxLayout* layout=dynamic_cast<QVBoxLayout*>( ui->local_song_list_widget->layout());
    if(!layout)
        return;

    int index=this->local_music_map.size();
    QString fileName=this->local_music_map[index].fileName();

    auto item=new MusicItemWidget(index,fileName.remove(fileName.lastIndexOf("."),fileName.length()),this);
    connect(item,&MusicItemWidget::playRequest,[this](int index){
        if(this->is_play)
            on_play_or_pause_toolButton_clicked();
        setPlayMusic(this->local_music_map[index]);
        on_play_or_pause_toolButton_clicked();
    });

    layout->insertWidget(layout->count()-1,item);
    ui->local_quantity_label->setText(QString::number(this->local_music_map.size()));

}


void KuGouApp::on_play_or_pause_toolButton_clicked()
{
    this->is_play=!this->is_play;

    if(this->is_play)
    {
        this->player->play();
        ui->play_or_pause_toolButton->setIcon(QIcon("://image/playbar/pause.svg"));
    }
    else
    {
        this->player->pause();
        ui->play_or_pause_toolButton->setIcon(QIcon("://image/playbar/play.svg"));
    }
}


void KuGouApp::on_minimize_toolButton_clicked()
{
    this->showMinimized();
}


void KuGouApp::on_maximize_toolButton_clicked()
{
    static bool is_max=false;
    if(is_max)
        this->showNormal();
    else
        this->showMaximized();
    is_max=!is_max;
}


void KuGouApp::on_close_window_toolButton_clicked()
{
    this->close();
}

